package com.example.javafx.bilibilidownload.util;


import com.example.javafx.bilibilidownload.constant.BiliVideoEnum;
import com.example.javafx.bilibilidownload.constant.Myinfo;
import com.example.javafx.bilibilidownload.pojo.BiliFavoriteSimplePojo;
import com.example.javafx.bilibilidownload.pojo.BiliFocusPojo;
import com.example.javafx.bilibilidownload.pojo.BiliUpPojo;
import com.example.javafx.bilibilidownload.pojo.BiliVideoPojo;
import com.example.javafx.bilibilidownload.service.BiliServiceImpl;
import com.example.javafx.bilibilidownload.task.BilibiliDownloadTask;
import com.example.javafx.bilibilidownload.task.ExecutorServiceForTransform;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


import java.util.ArrayList;
import java.util.List;

import static com.example.javafx.bilibilidownload.constant.VideoConstant.biliDownloadThreadPoolSize;
import static com.example.javafx.bilibilidownload.util.BiliFileUtil.biliDefaultPageSize;


/**
 * @Description: 下载up的全部视频
 * @Param:
 * @return:
 * @Author: Mr.zengbang
 * @Date: 2020/12/9
 */
public class BiliDownloadUtil {

    private static final Logger log = LogManager.getLogger(BiliDownloadUtil.class);

    private static BiliServiceImpl bilibiliService = new BiliServiceImpl();

    public static void main(String[] args) throws Exception {

        // 手动下载一个up的所有视频
//        String upId = "570095551";
//        List<String> keywordFiltering = new ArrayList<>();
//        downloadUp(upId, 0, "1996-1-1", keywordFiltering);

        // 下载我收藏的视频，所有分组所有视频
/*        String favoriteName = "下载";
        downloadMyFavoriteByName(favoriteName);
        downloadMyAllFavorite();*/

        // 下载集合视频
/*        String bvid = "BV1p84y1P7Z5";
        String collectionName = "前端";
        downloadCollection(bvid, collectionName);*/

        // 下载我关注分组里的所有up视频
/*        List<BiliFocusPojo> myfocus = bilibiliService.refreshMyfocus(Myinfo.id);
        myfocus.forEach(index -> {
            bilibiliService.refreshFocusUpList(index);
        });
        List<BiliUpPojo> focusUpList = bilibiliService.getFocusUpList("下载");
        for (BiliUpPojo biliUpPojo : focusUpList) {
            List<String> keywordFiltering = new ArrayList<>();
            downloadUp(biliUpPojo.getUpId(), biliUpPojo.getUpName(), 10000, "1996-1-1", keywordFiltering);
        }*/

    }

//    public static void downloadCollection(String bvid, String collectionName) {
//        bilibiliService.refreshPageList("", bvid, collectionName, "", BiliVideoEnum.Single);
//        List<BiliVideoPojo> singleDownloadVideo = bilibiliService.getSingleDownloadVideo(bvid, collectionName);
//        bilibiliService.fillVideoAndAudioInfos(singleDownloadVideo,
//                BiliVideoEnum.Single, BiliFileUtil.getSingleErrorFile(), true);
//        singleDownloadVideo.forEach(index -> {
//            BiliUtil.makeVideoNameByNull(index);
//        });
//        donwloadByExecutor(singleDownloadVideo, BiliVideoEnum.Single);
//    }

    public static void downloadUp(String upId, String upName, int minPlay, String beginTime, List<String> keywordFiltering) {
        List<BiliVideoPojo> biliVideoPojos = bilibiliService.refreshUPAllVideoList(upId, upName);
        // 找到author最多的应该就是up主名字
        List<BiliVideoPojo> downloadVideoByUpName = bilibiliService.getUPAllVideoList(upName, minPlay,
                beginTime, keywordFiltering);
        downloadVideoByUpName.forEach(index -> {
            BiliUtil.makeVideoNameByCreateTime(index);
        });
        String errorFileName = BiliFileUtil.getUpSpaceErrorFile(upName);
        bilibiliService.fillVideoAndAudioInfos(downloadVideoByUpName, BiliVideoEnum.Up, errorFileName, true);
        donwloadByExecutor(downloadVideoByUpName, BiliVideoEnum.Up);
    }

//    public static void downloadMyFavoriteByName(String favoriteName) throws Exception {
//        // 下载我的收藏夹所有视频
//        List<BiliFavoriteSimplePojo> myFavoriteList = bilibiliService.refreshMyFavorite();
//        myFavoriteList.stream().filter(index -> index.getTitle().equals(favoriteName)).forEach(index -> {
//            int pageNum = index.getCount() / biliDefaultPageSize + 1;
//            for (int i = 1; i <= pageNum + 1; i++) {
//                bilibiliService.refreshMyFavoriteDetail(index);
//            }
//        });
//        List<BiliVideoPojo> downloadVideoByUpName = bilibiliService.getDownloadVideoInFavoriteByName(favoriteName);
//        downloadVideoByUpName.forEach(index -> {
//            BiliUtil.makeVideoNameByFavoriteTime(index);
//        });
//        String errorFileName = BiliFileUtil.getFavoriteErrorFile();
//        bilibiliService.fillVideoAndAudioInfos(downloadVideoByUpName, BiliVideoEnum.Favorite, errorFileName, true);
//        donwloadByExecutor(downloadVideoByUpName, BiliVideoEnum.Favorite);
//    }

//    public static void downloadMyAllFavorite() throws Exception {
//        // 下载我的收藏夹所有视频
//        List<BiliFavoriteSimplePojo> myFavoriteList = bilibiliService.refreshMyFavorite();
//        myFavoriteList.forEach(index -> {
//            int pageNum = index.getCount() / biliDefaultPageSize + 1;
//            for (int i = 1; i <= pageNum + 1; i++) {
//                bilibiliService.refreshMyFavoriteDetail(index);
//            }
//        });
//        List<BiliVideoPojo> downloadVideoByUpName = bilibiliService.getDownloadVideoInFavorite();
//        downloadVideoByUpName.forEach(index -> {
//            BiliUtil.makeVideoNameByFavoriteTime(index);
//        });
//        String errorFileName = BiliFileUtil.getFavoriteErrorFile();
//        bilibiliService.fillVideoAndAudioInfos(downloadVideoByUpName, BiliVideoEnum.Favorite, errorFileName, true);
//        donwloadByExecutor(downloadVideoByUpName, BiliVideoEnum.Favorite);
//    }

    /**
     * @Description: 下载器，把要下载的视频列表放进去就行
     * @Author: bangbang23333
     * @Date: 2024/8/8 10:57
     */
    public static void donwloadByExecutor(List<BiliVideoPojo> videoList, BiliVideoEnum biliVideoEnum) {
        ExecutorServiceForTransform executorServiceForTransform = new ExecutorServiceForTransform(biliDownloadThreadPoolSize);
        if (videoList.isEmpty()) return;
        executorServiceForTransform.setCount(videoList.size());
        float count = 1;
        String upName = "";

        // 只有下载up视频的时候才有up名字
        if (biliVideoEnum.isUp()) {
            upName = BiliUtil.getUpname(videoList);
        }
        // 输出开始日志
        for (BiliVideoPojo bilibiliVideo : videoList) {
            log.info("当前处理到第{}个文件,下载到{}%", count, count++ / videoList.size() * 100);
            while (!executorServiceForTransform.check()) {
//                try {
//                    Thread.sleep(300);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
            }
            BilibiliDownloadTask bilibiliDownloadTask = new BilibiliDownloadTask(bilibiliVideo,
                    executorServiceForTransform.getSuccess(), upName, biliVideoEnum);
            executorServiceForTransform.addThread(bilibiliDownloadTask);
        }
        // 清空成功数
        executorServiceForTransform.shutDown();
    }
}

